Thread: C program: How do I validate the length of user input?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    C program: How do I validate the length of user input?

    For the following program, I'd like to make sure the user input is between 1 and 40 characters. To do this, how would I complete the if statement below?

    Thank you.

    Code:
     char p_input[41];
       
       printf("Enter a name (1-40 characters):");
       fgets(p_input, 41, stdin);
       
       if (p_input ....??

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't believe you can.

    This smells like a test question, and I'm sure that (as far as the instructor is concerned) the key fact is that fgets includes the \n (ie Enter-key) as part of the input; and so if the \n exists in the input you can be sure that the user typed less than 40 characters. If the user types 40 or more characters, there's no room for the \n (or the extra characters) and so it is not included. However, you cannot distinguish between 40 (which is okay) and "or more" (which is not).

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since you're using fgets() you've already insured that at most 41 characters will be accepted into your string. That is the purpose of the second parameter to limit the number of characters this function will retrieve. If you want to see if the user actually entered more than the 41 characters you need to check for the end of line character in your string. If it is not present then the user entered more than the desired characters. If it's present then the user entered 41 or fewer characters and you can check the length with strlen().


    Jim

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tabstop View Post
    I don't believe you can.

    This smells like a test question, and I'm sure that (as far as the instructor is concerned) the key fact is that fgets includes the \n (ie Enter-key) as part of the input; and so if the \n exists in the input you can be sure that the user typed less than 40 characters. If the user types 40 or more characters, there's no room for the \n (or the extra characters) and so it is not included. However, you cannot distinguish between 40 (which is okay) and "or more" (which is not).
    I like your explanation, but I disagree with the statement "I don't believe you can". I'll PM you some source code because I don't really want to put it here

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Read the documentation for fgets(). It explains the behaviour and return values of fgets() if the user enters more than the specified number of characters.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    1. If you're on Linux

    man 3 fgets

    2. If you're on Windows, install Cygwin and then go to 1

    3. If you later need help with a function other than fgets, go to 1 but replace fgets with the function you need help with

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The "I don't believe you can" was more of "finishing the if-statement". Although I imagine there is certainly a way in C to cram a check for 40 characters, doing another read, and then checking that new info all inside a single if-statement, I would not want to have to write it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validate user input
    By ulti-killer in forum C Programming
    Replies: 17
    Last Post: 07-12-2012, 11:09 PM
  2. Replies: 9
    Last Post: 06-16-2012, 09:18 AM
  3. Validate input value
    By aladin in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 09:41 AM
  4. How to Validate an Input
    By slowcoder in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 07:33 PM
  5. Validate a user input integer?
    By criticalerror in forum C++ Programming
    Replies: 20
    Last Post: 12-07-2003, 08:30 PM